package mroute

import (
	"github.com/gin-gonic/gin"
	"mygo/mystruct"
	"mygo/mymongo"
	"encoding/json"
	"mygo/mroute/utils"
	"fmt"
	"mygo/global"
)

/* =================================================================================
   用户API接口
*/
// 登录
func MyLogin(cont *gin.Context) {
	account := cont.PostForm("account")
	password := cont.PostForm("password")
	Ipstring := utils.GetClientIP(cont)
	UserAgent := cont.GetHeader("User-Agent")
	ret := mymongo.Login_User(account, password, Ipstring, UserAgent, global.Mtoken.Key)
	cont.JSON(mystruct.StatusOK, gin.H{"code": ret.Code, "msg": ret.Mesg})
}
// 登出
func MyLogout(cont *gin.Context) {
	account := cont.PostForm("account")
	Ipstring := utils.GetClientIP(cont)
	UserAgent := cont.GetHeader("User-Agent")
	UserNameBoot := cont.GetHeader("UserNameBoot")
	ret := mymongo.Logout_User(account, UserNameBoot, Ipstring, UserAgent, global.Mtoken.Key)
	cont.JSON(mystruct.StatusOK, gin.H{"code": mystruct.MyCodeOk, "msg": ret})
}
// 注册
func MyUserRegister(cont *gin.Context) {
	account  := cont.PostForm("account")
	password := cont.PostForm("password")
	standing := mystruct.StrToInt64(cont.DefaultPostForm("standing", "2"))

	var user *mystruct.Muser
	if global.Mtoken.Initcode == cont.PostForm("key") {
		user = &mystruct.Muser{
			Account: "初始秘钥",
			Standing: mystruct.AUTH_ININUSER,
		}
	}else{
		user = mymongo.Get_Chcae_User(cont.GetHeader("UserNameBoot"), global.Mtoken.Key)
	}
	if user == nil {
		cont.JSON(mystruct.StatusOK, gin.H{"code": mystruct.MyCodeErr, "msg": "没有权限创建[用户未登录]"})
		return
	}
	if user.Standing > standing {
		ret := mymongo.Register_User(account, user.Account, password, standing)
		cont.JSON(mystruct.StatusOK, gin.H{"code": ret.Code, "msg": ret.Mesg})
	}else{
		cont.JSON(mystruct.StatusOK, gin.H{"code": mystruct.MyCodeErr, "msg": "没有权限创建[用户权限不足]"})
	}
}
// 删除
func MyUserDelete(cont *gin.Context) {
	account := cont.PostForm("account")
	
	var user *mystruct.Muser
	if global.Mtoken.Initcode == cont.PostForm("key") {
		user = &mystruct.Muser{
			Account: "初始秘钥",
			Standing: mystruct.AUTH_ININUSER,
		}
	}else{
		user = mymongo.Get_Chcae_User(cont.GetHeader("UserNameBoot"), global.Mtoken.Key)
	}
	if user != nil {
		ret := mymongo.Delete_User(account, user)
		cont.JSON(mystruct.StatusOK, gin.H{"code": ret.Code, "msg": ret.Mesg})
	}else{
		cont.JSON(mystruct.StatusOK, gin.H{"code": mystruct.MyCodeErr, "msg": "没有权限删除[用户未登录]"})
	}
}
// 修改用户信息和权限
func MyUserData(cont *gin.Context) {
	account := cont.PostForm("account")
	value := cont.PostForm("value")
	code := cont.PostForm("code")
	
	var user *mystruct.Muser
	if global.Mtoken.Initcode == cont.PostForm("key") {
		user = &mystruct.Muser{
			Account: "初始秘钥",
			Standing: mystruct.AUTH_ININUSER,
		}
	}else{
		user = mymongo.Get_Chcae_User(cont.GetHeader("UserNameBoot"), global.Mtoken.Key)
	}
	if user == nil {
		cont.JSON(mystruct.StatusOK, gin.H{"code": mystruct.MyCodeErr, "msg": "没有权限[用户未登录]"})
		return
	}
	switch code {
		case "set":
			var json_data map[string]interface{}
			json.Unmarshal([]byte(value), &json_data)
			val_join := cont.PostForm("field")
			if val_join == "authority" {
				var setvalue interface{}
				val_join = fmt.Sprintf("authority.%s.%s", json_data["select"], json_data["field"])
				setvalue = json_data["value"]
				ret := mymongo.Set_UserAuth(account, val_join, setvalue, json_data["select"].(string), user)
				cont.JSON(mystruct.StatusOK, gin.H{"code": ret.Code, "msg": ret.Mesg})
			}else{
				ret := mymongo.Set_User(account, json_data, user)
				cont.JSON(mystruct.StatusOK, gin.H{"code": ret.Code, "msg": ret.Mesg})
			}

		case "add":
			ret := mymongo.Add_UserAuth(account, value, user)
			cont.JSON(mystruct.StatusOK, gin.H{"code": ret.Code, "msg": ret.Mesg})

		case "del":
			ret := mymongo.Del_UserAuth(account, value, user)
			cont.JSON(mystruct.StatusOK, gin.H{"code": ret.Code, "msg": ret.Mesg})

		case "see":
			limit := mystruct.StrToInt64(cont.DefaultPostForm("limit","30"))
			page  := mystruct.StrToInt64(cont.DefaultPostForm("page","0"))
			var json_data map[string]interface{}
			json.Unmarshal([]byte(value), &json_data)
			ret, res := mymongo.List_User(limit, page, json_data, user)
			cont.JSON(mystruct.StatusOK, gin.H{"code": ret.Code, "msg": res})
			
		default:
			cont.JSON(mystruct.StatusOK, gin.H{"code": mystruct.MyCodeErr, "msg": "参数错误"})
	}
}